package com.yjs.app.trade.service.service.business.member.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.core.base.mybatis.dbutil.DynamicSwitchDataSource;
import com.yjs.app.trade.core.util.PinYinUtil;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.base.util.Page;
import com.yjs.app.trade.entity.bean.CompanySearch;
import com.yjs.app.trade.entity.bean.MemberSearch;
import com.yjs.app.trade.service.mapper.business.member.MemberMapper;
import com.yjs.app.trade.service.service.business.member.MemberService;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

	@Autowired
	MemberMapper memberMapper;

	@Override
	public Object findDispatchCompany(Page<CompanySearch> page, CompanySearch companySearch) {
		companySearch.setPage(page);
		page.setMaps(memberMapper.findDispatchCompany(companySearch));
		return page;
	}

	@Override
	public Object findSellerCompany(Page<CompanySearch> page, CompanySearch companySearch) {
		companySearch.setPage(page);
		page.setMaps(memberMapper.findSellerCompany(companySearch));
		return page;
	}

	@Override
	public Map<String, List<Map<String, Object>>> findPlatFormGeoList() {
		List<Map<String, Object>> list = memberMapper.findPlatFormGeoList();
		Map<String, List<Map<String, Object>>> maps = new HashMap<String, List<Map<String, Object>>>();
		for (Map<String, Object> map : list) {
			if (!setRegionOtherPinyin(maps, map)) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (StringUtils.equals(entry.getKey(), "regionName") || 
							StringUtils.equals(entry.getKey(), "geoName")) {
						if (maps.get(PinYinUtil.getFirstValue(entry.getValue().toString())) != null) {
							maps.get(PinYinUtil.getFirstValue(entry.getValue().toString())).add(map);
						} else {
							List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
							listMap.add(map);
							maps.put(PinYinUtil.getFirstValue(entry.getValue().toString()), listMap);
						}
					}
				}
			}
		}
		return maps;
	}

	@Override
	public Map<String, Object> getRegionById(String regionId) {
		return memberMapper.getRegionById(regionId);
	}

	@Override
	public List<Map<String, Object>> getRegionList(String parentId) {
		return memberMapper.getRegionList(parentId);
	}
	
	@Override
	public Map<String, List<Map<String, Object>>> getRegionListSort(String parentId) {
		List<Map<String, Object>> list = memberMapper.getRegionList(parentId);
		Map<String, List<Map<String, Object>>> maps = new HashMap<String, List<Map<String, Object>>>();
		for (Map<String, Object> map : list) {
			if (!setRegionOtherPinyin(maps, map)) {
				for (Map.Entry<String, Object> entry : map.entrySet()) {
					if (StringUtils.equals(entry.getKey(), "regionName") || 
							StringUtils.equals(entry.getKey(), "geoName")) {
						if (maps.get(PinYinUtil.getFirstValue(entry.getValue().toString())) != null) {
							maps.get(PinYinUtil.getFirstValue(entry.getValue().toString())).add(map);
						} else {
							List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
							listMap.add(map);
							maps.put(PinYinUtil.getFirstValue(entry.getValue().toString()), listMap);
						}
					}
				}
			}
		}
		return maps;
	}
	
	private boolean setRegionOtherPinyin (Map<String, List<Map<String, Object>>> maps,Map<String, Object> map) {
		String pinyin = "";
		boolean flag = false;
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			if (StringUtils.equals(entry.getKey(), "regionName") || 
					StringUtils.equals(entry.getKey(), "geoName")) {
				String str = entry.getValue().toString().substring(0, 1);
				if (StringUtils.equals(str, "綦")) {
					pinyin = "q";
					flag = true;
				} else if (StringUtils.equals(str, "潼")) {
					pinyin = "t";
					flag = true;
				} else if (StringUtils.equals(str, "璧")) {
					pinyin = "b";
					flag = true;
				}
			} else if (StringUtils.equals(entry.getKey(), "regionCode") || 
					StringUtils.equals(entry.getKey(), "plantformGeoId")) {
				String regionCode = entry.getValue().toString();
				if (StringUtils.equals(regionCode, "500222") || 
						StringUtils.equals(regionCode, "GEO_CQ18")) {
					pinyin = "q";
					flag = true;
				} else if (StringUtils.equals(regionCode, "500223") || 
						StringUtils.equals(regionCode, "GEO_CQ19")) {
					pinyin = "t";
					flag = true;
				} else if (StringUtils.equals(regionCode, "500227") ||
						StringUtils.equals(regionCode, "GEO_CQ23")) {
					pinyin = "b";
					flag = true;
				}
			}
			if (flag) {
				if (maps.get(pinyin) != null) {
					maps.get(pinyin).add(map);
				} else {
					List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
					listMap.add(map);
					maps.put(pinyin, listMap);
				}
				return flag;
			}
		}
		return flag;
	}

	@Override
	public List<Map<String, Object>> getRegionParents() {
		return memberMapper.getRegionParents();
	}

	@Override
	public Map<String, Object> findMemenberId(String number) {
		
		return memberMapper.findMemenberId(number);
	}

	@Override
	public List<Map<String, Object>> findDispatchBySeller(String sellerId) {
		return memberMapper.findDispatchBySeller(sellerId);
	}

	@Override
	public Page<?> findMembersByUnionMemberId(Page<MemberSearch> page, MemberSearch memberSearch) {
		memberSearch.setPage(page);
		List<Map<String, Object>> list = memberMapper.findMembersByUnionMemberId(memberSearch);
		page.setMaps(list);
		return page;
	}

	@Override
	public CompanySearch findUnionMemberById(MemberSearch memberSearch) {
		return memberMapper.findUnionMemberById(memberSearch);
	}

	@Override
	public Page<?> findMemberAppVSAppPackage(String memberId) {
		Page<MemberSearch> page = new Page<>();
		page.setPageNo(1);
		page.setPageSize(10000);
		MemberSearch memberSearch = new MemberSearch();
		memberSearch.setMemberId(memberId);
		memberSearch.setPage(page);
		List<Map<String, Object>> list = memberMapper.findMemberAppVSAppPackage(memberSearch);
		page.setMaps(list);
		return page;
	}

	@Override
	public Integer findMemberAppVSIsExpire(String memberId) {
		return memberMapper.findMemberAppVSIsExpire(memberId);
	}
	
	@Override
	@DynamicSwitchDataSource(dataSource = "yjsbd")
	public List<Map<String, Object>> findIntegrity(Map<String, Object> map) {	
		return memberMapper.findIntegrity(map);	
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "yjsbd")
	public Map<String, Object> findIntegrityInformation(Map<String, Object> map) {
		return memberMapper.findIntegrityInformation(map);
	}

	@Override
	@DynamicSwitchDataSource(dataSource = "yjsbd")
	public Map<String, Object> findIntegrityScope(Map<String, Object> map) {
		return memberMapper.findIntegrityScope(map);
	}
}
